package com.Sdebruijn.curriculumvitae;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.Sdebruijn.curriculumvitae.asynctask.LoadAppDataAsyncTask;
import com.Sdebruijn.curriculumvitae.listeners.OnDoneLoadingListener;
import com.Sdebruijn.curriculumvitae.util.OnClickUtils;

/**
 * Activity showing a list of apps on my personal account
 * 
 * @author Stefan de Bruijn
 * 
 */
public class AppsActivity extends BaseActivity implements OnDoneLoadingListener {
	private LoadAppDataAsyncTask mLoadAppDataAsyncTask;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_apps);
		getSupportActionBar().setTitle("Apps");
		getSupportActionBar().setLogo(R.drawable.icon_android);

		RelativeLayout rl_apps_data = (RelativeLayout) findViewById(R.id.rl_apps_data);

		mLoadAppDataAsyncTask = new LoadAppDataAsyncTask(rl_apps_data, this, this);
		mLoadAppDataAsyncTask.execute();

		// LinearLayout ll_apps_data = (LinearLayout)
		// findViewById(R.id.ll_apps_data);
		// ll_apps_data.getChildAt(0);
	}

	@Override
	public void onDoneLoading(View view) {
		LinearLayout ll_apps_data = (LinearLayout) view;
		// TODO Seems pretty hacky : update this later into a cleaner way.
		for (int i = 0; i < ll_apps_data.getChildCount(); i++) {
			LinearLayout child = (LinearLayout) ll_apps_data.getChildAt(i);
			// The LinearLayout which holds the whole visible block is always
			// child(0)
			View innerChild = child.getChildAt(0);
			innerChild.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					TextView tv_app_block_link = (TextView) v.findViewById(R.id.tv_app_block_link);
					OnClickUtils.openPlayStore(AppsActivity.this, tv_app_block_link.getText()
							.toString());
				}
			});
		}
	}

}
